package com.zyh.flink.day06.function;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.List;

public class AggregateAndProcessFunctionJob {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> dataStreamSource = environment.socketTextStream("hadoop10", 9999);

        KeyedStream<Tuple2<String, Integer>, String> keyedStream = dataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] ss = value.split("\\s+");
                out.collect(Tuple2.of(ss[0], Integer.valueOf(ss[1])));
            }
        }).keyBy(t -> t.f0);

        WindowedStream<Tuple2<String, Integer>, String, TimeWindow> windowedStream = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(10)));

        SingleOutputStreamOperator<String> result = windowedStream.aggregate(new AggregateFunction<Tuple2<String, Integer>, Tuple3<String, Double, Integer>, String>() {
            @Override
            public Tuple3<String, Double, Integer> createAccumulator() {
                return Tuple3.of("", 0.0, 0);
            }

            @Override
            public Tuple3<String, Double, Integer> add(Tuple2<String, Integer> value, Tuple3<String, Double, Integer> accumulator) {
                accumulator.setFields(value.f0, accumulator.f1 + value.f1, accumulator.f2 + 1);
                return accumulator;
            }

            @Override
            public String getResult(Tuple3<String, Double, Integer> accumulator) {
                return accumulator.f0 + "消费了" + accumulator.f2 + "次,平均消费" + (accumulator.f1 / accumulator.f2);
            }

            @Override
            public Tuple3<String, Double, Integer> merge(Tuple3<String, Double, Integer> a, Tuple3<String, Double, Integer> b) {
                return Tuple3.of(a.f0, a.f1 + b.f1, a.f2 + b.f2);
            }
        }, new ProcessWindowFunction<String, String, String, TimeWindow>() {
            @Override
            public void process(String s, ProcessWindowFunction<String, String, String, TimeWindow>.Context context, Iterable<String> elements, Collector<String> out) throws Exception {
                TimeWindow window = context.window();
                long start = window.getStart();
                long end = window.getEnd();
                System.out.println("start = " + start + ",end = " + end);

                List<String> list = new ArrayList<>(1);
                elements.forEach(element -> list.add(element));

                out.collect(list.get(0));
            }
        });

        result.print();

        environment.execute("aggregateAndProcessFunction");
    }
}